const axios = require('axios');
import { APISERVER } from '../../config'
class http {
    constructor() {
        this.param = {
            method: 'get',
            url: '',
            data: {},
            param: {},
            timeout: 10000
        }
    }
    /**
     * 
     * @param {地址} url 
     * @param {参数} param 
     */
    post(uri, param) {
        this.param.data = param
        this.param.params = param
        this.param.method = "post"
        this.param.url = APISERVER + '/' + uri
        return this.request()
    }
    /**
     * 
     * @param {地址} url 
     * @param {参数} param 
     */
    get(uri, param) {
        this.param.data = param
        this.param.params = param
        this.param.method = "get"
        this.param.url = APISERVER + '/' + uri
        return this.request()
    }
    /**
     * 
     * @param {地址} url 
     * @param {参数} param 
     */
    put(uri, param) {
        this.param.data = param
        this.param.params = param
        this.param.method = "put"
        this.param.url = APISERVER + '/' + uri
        return this.request()
    }
    /**
     * 
     * @param {地址} url 
     * @param {参数} param 
     */
    delete(uri, param) {
        this.param.data = param
        this.param.params = param
        this.param.method = "delete"
        this.param.url = APISERVER + '/' + uri
        return this.request()
    }
    /**
     * 
     * @returns promise类型的axios
     */
    request() {

        this.param.headers = { token: localStorage.getItem('token') }
        return new Promise((resolve, reject) => {
            axios(this.param).then(e => {
                // if (e.data.code != 200) alert(e.data.msg)
                resolve(e.data)
            })
        })
    }
}

export default new http()